using System;
using System.Text;

namespace SMS.Gateway.Libs
{
    public class HexDecoder : Decoder
    {
        // Methods
        internal HexDecoder() { }

        private static char GetChar(byte b)
        {
            if (b <= 9)
                return (char)(0x30 + b);
            
            return (char)(0x41 + (b - 10));
        }

        public override int GetCharCount(byte[] bytes, int index, int maxBytes)
        {
            return (Math.Min(bytes.Length - index, maxBytes) * 2);
        }

        public char[] GetChars(byte[] bytes)
        {
            char[] chars = new char[this.GetCharCount(bytes, 0, bytes.Length)];
            this.GetChars(bytes, 0, bytes.Length, chars, 0);
            return chars;
        }

        public override int GetChars(byte[] bytes, int byteIndex, int maxBytes, char[] chars, int charIndex)
        {
            int num = this.GetCharCount(bytes, byteIndex, maxBytes);
            if ((chars.Length - charIndex) < num)
                throw new ArgumentException("The character array is not large enough to contain the characters that will be generated from the byte buffer.", "chars");
            
            int index = byteIndex;
            while (index < maxBytes)
            {
                byte b = (byte)Math.Floor((double)(bytes[index] / 0x10));
                byte num4 = (byte)(bytes[index] % 0x10);
                chars[charIndex] = GetChar(b);
                chars[charIndex + 1] = GetChar(num4);
                index++;
                charIndex += 2;
            }
            return num;
        }
    }
}

